package com.jc.order.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import com.jc.constant.PolicyAttachmentTypeConst;
import com.jc.order.common.DictionaryToJcConst;
import com.jc.order.constant.DictConst;
import com.jc.order.constant.OrderConst;
import com.jc.order.constant.ValidateErrorConst;
import com.jc.order.dto.*;
import com.jc.order.service.CargoValidateService;
import com.jc.order.service.CountryService;
import com.jc.order.service.ExchangeRateService;
import com.jc.order.service.SystemDictDataService;
import com.jc.util.Result;
import com.jc.vo.PolicyRequest;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service("cargoValidateServiceImpl")
@Slf4j
public abstract class CargoValidateServiceImpl implements CargoValidateService {


    @Autowired
    private CountryService countryService;
    @Autowired
    private SystemDictDataService systemDictDataService;
    @Autowired
    private ExchangeRateService exchangeRateService;

    // 运输方式校验
    protected void validateTransportType(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getTransportModeCode())) {
            Object value = processPackingCode(request.getTransportModeCode(), DictionaryToJcConst.TRANSPORTTYPE_MAP, errorList, "干线运输方式");
            if (value != null) {
                dto.setTransportType(Integer.valueOf(String.valueOf(value)));
            }
        }
    }

    // 包装类型校验
    protected void validateGoodsPackage(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getPackingCode())) {
            Object value = processPackingCode(request.getPackingCode(), DictionaryToJcConst.PACKING_MAP, errorList, "包装类型");
            if (value != null) {
                dto.setGoodsPackage(String.valueOf(value));
            }
        }
    }


    protected Object processPackingCode(String packingCode, Map dataMap, List<String> errorList, String text) {
        Object value = dataMap.get(packingCode.trim());
        if (value == null) {
            errorList.add(String.format(ValidateErrorConst.INVALID_STR, text, packingCode));
        }
        return value;
    }

    /**
     * 被保险人校验
     */
    protected void insuredValid(List<String> errorList, JcPolicyDTO p) {
        String insuredName = p.getInsured();
        if (StrUtil.isEmpty(insuredName)) {
            errorList.add(ValidateErrorConst.ERROR_INSURED_EMPTY);
        } else if (insuredName.length() > 255) {
            errorList.add(ValidateErrorConst.ERROR_INSURED_LENGTH);
        }
        if (p.getInsuredType() != null) {
            if (OrderConst.INDIVIDUAL_TYPE.equals(p.getInsuredType())) {
                if (StrUtil.isEmpty(p.getInsuredId())) {
                    errorList.add(ValidateErrorConst.ERROR_INSURED_ID_EMPTY);
                }
            }
        } else {
            errorList.add(ValidateErrorConst.ERROR_INSURED_TYPE_EMPTY);
        }
        if (StrUtil.isNotBlank(p.getInsuredAddress()) && p.getInsuredAddress().length() > 200) {
            errorList.add(ValidateErrorConst.ERROR_INSURED_ADDRESS_LENGTH);
        }
    }

    /**
     * 运输方式校验
     */
    protected void transportModeValid(List<String> errorList, JcPolicyDTO p) {
        Integer transportType = p.getTransportType();
        if (transportType == null) {
            errorList.add(ValidateErrorConst.ERROR_TRANSPORT_TYPE_EMPTY);
        } else {
            boolean isValid = systemDictDataService.selectDictDataByDictType(DictConst.DICT_TRANSPORT_TYPE).stream()
                    .anyMatch(d -> transportType.toString().equals(d.getValue()));
            if (!isValid) {
                errorList.add(ValidateErrorConst.ERROR_TRANSPORT_TYPE_INVALID);
            }
        }
    }

    /**
     * 保额确定方式校验
     */
    protected void validateBaseAmountWay(List<String> errorList, JcPolicyDTO p) {
        if (p.getInsuranceConfirmType() == null) {
            errorList.add(ValidateErrorConst.ERROR_AMOUNT_WAY_EMPTY);
        } else {
            switch (p.getInsuranceConfirmType()) {
                case 1:
                    ratioValid(errorList, p);
                    p.setDeliveryAmount(null);
                    p.setDeliveryCurrency(null);
                    break;
                case 2:
                    p.setAppendPercent(new BigDecimal(100));
                    break;
                default:
                    errorList.add(ValidateErrorConst.ERROR_AMOUNT_WAY_INVALID);
            }
        }
    }

    /**
     * 加成比例校验
     */
    protected void ratioValid(List<String> errorList, JcPolicyDTO p) {
        if (p.getAppendPercent() == null || p.getAppendPercent().compareTo(BigDecimal.ZERO) != 1) {
            errorList.add(ValidateErrorConst.ERROR_RATIO_INVALID);
        }
    }

    /**
     * 货值校验
     */
    protected void cargoValueValid(List<String> errorList, JcPolicyDTO p) {
        if (p.getGoodsValueAmount() == null || p.getGoodsValueAmount().compareTo(BigDecimal.ZERO) != 1) {
            errorList.add(ValidateErrorConst.ERROR_CARGO_VALUE_EMPTY);
        }
    }

    /**
     * 币种校验
     */
    protected void currencyValid(List<String> errorList, JcPolicyDTO p) {
        String currency = p.getCurrency();
        if (StrUtil.isEmpty(currency)) {
            errorList.add(ValidateErrorConst.ERROR_CURRENCY_EMPTY);
        } else {
            if (isChineseYuan(currency)) {
                p.setExchangeRate(BigDecimal.ONE);
                p.setDeliveryCurrency(OrderConst.CURRENCY_RMB);
            } else {
                SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_CURRENCY, currency);
                if (dictData == null) {
                    errorList.add(ValidateErrorConst.ERROR_CURRENCY_INVALID);
                } else {
                    Result<BigDecimal> rateResult = exchangeRateService.getExchangeRate(currency);
                    if (!rateResult.getSuccess()) {
                        errorList.add(rateResult.getMessage());
                    }
                    p.setExchangeRate(rateResult.getContent());
                }
            }
        }
    }

    /**
     * 原单号长度校验
     */
    protected void trackingNoLengthValid(List<String> errorList, JcPolicyDTO p) {
        if (StrUtil.isNotBlank(p.getOrderNumber()) && p.getOrderNumber().length() > 60) {
            errorList.add(ValidateErrorConst.ERROR_TRACKING_NO_LENGTH);
        }
    }

    /**
     * 运输工具校验
     */
    protected void blnameValid(List<String> errorList, JcPolicyDTO p) {
        String transportName = p.getTransportName();
        if (StrUtil.isEmpty(transportName)) {
            errorList.add(ValidateErrorConst.ERROR_TRANSPORT_NAME_EMPTY);
        } else if (transportName.length() > 40) {
            errorList.add(ValidateErrorConst.ERROR_TRANSPORT_NAME_LENGTH);
        }
    }

    /**
     * 运单号校验
     */
    protected void waybillNumberValid(List<String> errorList, JcPolicyDTO p) {
        if (StrUtil.isEmpty(p.getWaybillNumber())) {
            if (!"5".equals(p.getTransportType())) {
                errorList.add(ValidateErrorConst.ERROR_WAYBILL_EMPTY);
            }
        } else if (p.getWaybillNumber().length() > 20) {
            errorList.add(ValidateErrorConst.ERROR_WAYBILL_LENGTH);
        }
    }

    /**
     * 公斤数校验
     */
    protected void chargeableWeight(List<String> errorList, JcPolicyDTO p) {
        BigDecimal weight = p.getWeightPackaged() != null ?
                p.getWeightPackaged().setScale(2, BigDecimal.ROUND_HALF_UP) : null;
        if (weight == null || weight.compareTo(BigDecimal.ZERO) != 1) {
            errorList.add(ValidateErrorConst.ERROR_WEIGHT_INVALID);
        } else if (weight.compareTo(new BigDecimal(5)) <= 0) {
            errorList.add(ValidateErrorConst.ERROR_WEIGHT_MIN);
        }
    }

    /**
     * 包装类型校验
     */
    protected void packingValid(List<String> errorList, JcPolicyDTO p) {
        String packingCode = p.getGoodsPackage();
        if (StrUtil.isEmpty(packingCode)) {
            errorList.add(ValidateErrorConst.ERROR_PACKING_EMPTY);
        } else if (systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_PACKAGE_TYPE, packingCode) == null) {
            errorList.add(ValidateErrorConst.ERROR_PACKING_INVALID);
        }
    }

    /**
     * 包装数量校验
     */
    protected void packingQuantityValid(List<String> errorList, JcPolicyDTO p) {
        if (p.getPackageCount() == null || p.getPackageCount() < 1) {
            errorList.add(ValidateErrorConst.ERROR_PACKAGE_COUNT);
        }
    }

    /**
     * 货物描述校验
     */
    protected void cargoDescValid(List<String> errorList, JcPolicyDTO p) {
        String desc = p.getGoodsNote();
        if (StrUtil.isEmpty(desc)) {
            errorList.add(ValidateErrorConst.ERROR_CARGO_DESC_EMPTY);
//        } else if (desc.length() > 500) {
//            errorList.add(ValidateErrorConst.ERROR_CARGO_DESC_LENGTH);
        }
    }

    /**
     * 起运国校验
     */
    protected void fromCountryValid(List<String> errorList, JcPolicyDTO p) {
        if (StrUtil.isEmpty(p.getFromCountry())) {
            errorList.add(ValidateErrorConst.ERROR_FROM_COUNTRY_EMPTY);
        } else {
            CountryDTO countryDTO = validateWarRiskCountry(p, errorList, p.getFromCountry(), "起运国");
            if (countryDTO != null) {
                p.setFromCountry(countryDTO.getCountryCode());
            }
        }
    }


    /**
     * 中转国
     *
     * @param errorList
     * @param p
     */
    protected void transferCountryValid(List<String> errorList, JcPolicyDTO p) {
        String countryCode = p.getTransferCountry();
        if (StrUtil.isNotEmpty(countryCode)) {
            CountryDTO countryDTO = this.validateWarRiskCountry(p, errorList, countryCode, "中转国");

            if (countryDTO != null) {
                p.setTransferCountry(countryDTO.getCountryCode());
            }
//        } else {
            // errorList.add("中转国代码不能为空");
        }
    }

    /**
     * 中转地址
     *
     * @param errorList
     * @param p
     */
    protected void transferAddressValid(List<String> errorList, JcPolicyDTO p) {
        String transferAddress = p.getTransferAddress();
        String countryCode = p.getTransferCountry();
        if (StrUtil.isNotEmpty(countryCode) && StrUtil.isEmpty(transferAddress)) {
            errorList.add("中转国不为空， 中转地址不能为空;");
        }
    }


    /**
     * 目的国
     *
     * @param errorList
     * @param p
     */
    protected void toCountryValid(List<String> errorList, JcPolicyDTO p) {
        String countryCode = p.getToCountry();
        if (StrUtil.isNotEmpty(countryCode)) {
            CountryDTO countryDTO = this.validateWarRiskCountry(p, errorList, countryCode, "目的国");
            if (countryDTO != null) {
                p.setToCountry(countryDTO.getCountryCode());
            }
        } else {
            errorList.add(ValidateErrorConst.ERROR_TO_COUNTRY_EMPTY);
        }
    }

    /**
     * 战争险国家校验
     */
    protected CountryDTO validateWarRiskCountry(JcPolicyDTO p, List<String> errorList, String countryCode, String type) {
        CountryDTO country = countryService.selectByCode(countryCode);
        if (country == null) {
            errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_MISMATCH, type));
            return null;
        }

        Boolean b = true;
        if (country.getForbidden()) {
            errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_FORBIDDEN, type + country.getCountryName()));
            b = false;
        }
        if (OrderConst.PRODUCT_CODE_2.equalsIgnoreCase(p.getProductCode())) {
            if (!country.getAvailableNew()) {
                errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_INNOVATION, type + country.getCountryName()));
                b = false;

            }
            if (p.getWarProtection() != null && p.getWarProtection() && !country.getAvailableWar()) {
                errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_WAR, type + country.getCountryName()));
                b = false;

            }
            if (p.getRiotProtection() != null && p.getRiotProtection() && !country.getAvailableStrikeInsurance()) {
                errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_STRIKE, type + country.getCountryName()));
                b = false;

            }
        }

        if (OrderConst.PRODUCT_CODE_1.equalsIgnoreCase(p.getProductCode())) {
            if (!country.getAvailableTraditional()) {
                errorList.add(String.format(ValidateErrorConst.ERROR_COUNTRY_TRADITIONAL, type + country.getCountryName()));
                b = false;
            }
        }
        return b ? country : null;
    }


    /**
     * 运费币种校验
     */
    protected void freightCurrencyValid(List<String> errorList, JcPolicyDTO p) {
        String currency = p.getDeliveryCurrency();
        Integer insuranceConfirmType = p.getInsuranceConfirmType();
        if (insuranceConfirmType == null || !"2".equals(insuranceConfirmType.toString())) return;

        if (StrUtil.isEmpty(currency)) {
            errorList.add(ValidateErrorConst.ERROR_FREIGHT_CURRENCY_EMPTY);
        } else {
            if (isChineseYuan(currency)) {
                p.setExchangeRate(BigDecimal.ONE);
                p.setDeliveryCurrency(OrderConst.CURRENCY_RMB);
            } else {
                SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_CURRENCY, currency);
                if (dictData == null) {
                    errorList.add(ValidateErrorConst.ERROR_FREIGHT_CURRENCY_INVALID);
                } else {
                    Result<BigDecimal> rateResult = exchangeRateService.getExchangeRate(currency);
                    if (!rateResult.getSuccess()) {
                        errorList.add(rateResult.getMessage());
                    }
                    p.setDeliveryExchangeRate(rateResult.getContent());
                }
            }
        }
    }

    /**
     * 运费校验
     */
    protected void freightValid(List<String> errorList, JcPolicyDTO p) {
        Integer insuranceConfirmType = p.getInsuranceConfirmType();
        if (insuranceConfirmType == null || !"2".equals(insuranceConfirmType.toString())) return;

        BigDecimal deliveryAmount = p.getDeliveryAmount();
        if (deliveryAmount == null || deliveryAmount.compareTo(new BigDecimal("0.01")) < 0) {
            errorList.add(ValidateErrorConst.ERROR_FREIGHT_AMOUNT_INVALID);
        }
    }


    /**
     * 起运日期 校验
     *
     * @param errorList
     * @param p
     */
    protected void startTimeValid(List<String> errorList, JcPolicyDTO p) {
        Date startTime = p.getStartTime();
        if (startTime == null) {
            errorList.add(ValidateErrorConst.ERROR_START_TIME_EMPTY);
        } else {

        }
    }


    /**
     * 起运地地址
     *
     * @param errorList
     * @param p
     */
    protected void fromAddressValid(List<String> errorList, JcPolicyDTO p) {
        String fromAddress = p.getFromAddress();
        if (StrUtil.isEmpty(fromAddress)) {
            errorList.add(ValidateErrorConst.ERROR_FROM_ADDRESS_EMPTY);
        }
    }

    /**
     * 目的地地址
     *
     * @param errorList
     * @param p
     */
    protected void toAddresssValid(List<String> errorList, JcPolicyDTO p) {
        String toAddress = p.getToAddress();
        if (StrUtil.isEmpty(toAddress)) {
            errorList.add(ValidateErrorConst.ERROR_TO_ADDRESS_EMPTY);
        }
    }


    //派送方式
    protected void validateDispatchType(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getPackingCode())) {
            Object value = processPackingCode(request.getDeliverywayCode(), DictionaryToJcConst.DELIVERYWAY_MAP, errorList, "派送方式");
            if (value != null) {
                dto.setDispatchType(Integer.valueOf(value.toString()));
            }
        }
    }


    // 上架保障
    protected void validateShelfGuarantee(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getShelfType())) {
            Object value = processPackingCode(request.getShelfType(), DictionaryToJcConst.SHELFTYPE_MAP, errorList, "上架保障");
            if (value != null) {
                dto.setShelfGuarantee(Integer.valueOf(value.toString()));
            }
        }
    }


    // 目的地类型校验
    protected void validateDestType(PolicyRequest request, JcPolicyDTO dto, List<String> errorList) {
        if (StrUtil.isNotBlank(request.getDestType())) {
            Object value = processPackingCode(request.getDestType(), DictionaryToJcConst.WAREHOUSE_MAP, errorList, "目的地类型");
            if (value != null) {
                dto.setDestType(Byte.valueOf(value.toString()));
            }
        }
    }


    /**
     * 派送方式校验
     */
    protected void deliverywayValueValid(List<String> errorList, JcPolicyDTO p) {
        Integer dispatchType = p.getDispatchType();
        if (dispatchType != null) {
            SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_DISPATCH_TYPE, dispatchType.toString());
            if (dictData == null) {
                errorList.add(ValidateErrorConst.ERROR_DISPATCH_TYPE_MISMATCH);
                return;
            }
            //保费计算 不验证 快递单号
            if (!OrderConst.PREMIUM_CALCULATION.equalsIgnoreCase(p.getLogic())) {
                if ("2".equalsIgnoreCase(dispatchType.toString())) {
                    if (StrUtil.isNotEmpty(p.getExpressCompany()) || StrUtil.isNotEmpty(p.getExpressTrackingNumber())) {
                        errorList.add(ValidateErrorConst.ERROR_TRUCK_DISPATCH_INVALID);
                    }
                }
                String expressNo = p.getExpressTrackingNumber();
                if ("1".equalsIgnoreCase(dispatchType.toString())) {
                    if (StrUtil.isEmpty(p.getExpressTrackingNumber()) || StrUtil.isEmpty(p.getExpressCompany())) {
                        errorList.add(ValidateErrorConst.ERROR_EXPRESS_INFO_EMPTY);
                    }
                    if (StrUtil.isNotEmpty(expressNo) && expressNo.length() > 350) {
                        errorList.add(ValidateErrorConst.ERROR_EXPRESS_NO_LENGTH);
                    }
                }
            }
        } else {
            errorList.add(ValidateErrorConst.ERROR_DISPATCH_TYPE_EMPTY);
        }
    }

    /**
     * 校验目的地类型
     */
    protected void destTypeValid(List<String> errorList, JcPolicyDTO p) {
        Byte destType = p.getDestType();
        if (destType == null) {
            errorList.add(ValidateErrorConst.ERROR_DEST_TYPE_EMPTY);
        } else {
            SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_DEST_TYPE, destType.toString());
            if (dictData == null) {
                errorList.add(ValidateErrorConst.ERROR_DEST_TYPE_MISMATCH);
            } else {
                if (!"其他海外仓".equalsIgnoreCase(dictData.getLabel()) && !"私人地址/商业地址".equalsIgnoreCase(dictData.getLabel())) {
                    warehousingNumberValid(errorList, p);
                } else{
                    Integer shelfGuarantee = p.getShelfGuarantee();
                    if (shelfGuarantee != null && shelfGuarantee!=3) {
                        errorList.add("目的地类型为"+dictData.getLabel()+ValidateErrorConst.ERROR_SHELF_GUARANTEE_3);
                    }
                }
            }
        }
    }

    /**
     * 上架保障校验
     */
    protected void isShelfValid(List<String> errorList, JcPolicyDTO p) {
        Integer shelfGuarantee = p.getShelfGuarantee();
        if (shelfGuarantee == null) {
            errorList.add(ValidateErrorConst.ERROR_SHELF_GUARANTEE_EMPTY);
        } else {
            SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_SHELF_GUARANTEE, shelfGuarantee.toString());
            if (dictData == null) {
                errorList.add(ValidateErrorConst.ERROR_SHELF_GUARANTEE_MISMATCH);
            }
        }
    }

    /**
     * 快递公司校验
     */
    protected void expressCompanyValid(List<String> errorList, JcPolicyDTO p) {
        String expressCompany = p.getExpressCompany();
        if (StrUtil.isNotBlank(expressCompany)) {
            SystemDictDataDTO dictData = systemDictDataService.selectDictDataByDictTypeAndValue(DictConst.DICT_EXPRESS_COMPANY, expressCompany);
            if (dictData == null) {
                errorList.add(ValidateErrorConst.ERROR_EXPRESS_COMPANY_MISMATCH);
            }
        }
    }

    /**
     * 入仓编号校验
     */
    protected void warehousingNumberValid(List<String> errorList, JcPolicyDTO p) {
        String warehousingNumber = p.getWarehousingNumber();
        if (StrUtil.isEmpty(warehousingNumber)) {
            errorList.add(ValidateErrorConst.ERROR_WAREHOUSING_NUMBER_EMPTY);
        }
//        if (StrUtil.isNotEmpty(warehousingNumber) && warehousingNumber.length() > 350) {
//            errorList.add(ValidateErrorConst.ERROR_WAREHOUSING_NUMBER_LENGTH);
//        }
    }


    /**
     * 判断是否人民币货币
     */
    private boolean isChineseYuan(String currency) {
        return StrUtil.isNotBlank(currency) &&
                (currency.equalsIgnoreCase(OrderConst.CURRENCY_CNY) || currency.equalsIgnoreCase(OrderConst.CURRENCY_RMB));
    }


    protected void getAttachment(JcPolicyDTO entity) {
        List<PolicyAttachmentDTO> allAttachment = entity.getAttachmentList();
        if (allAttachment != null && allAttachment.size() > 0) {
            List<PolicyAttachmentDTO> cargoDescAttachmentList = new ArrayList<>();
            List<PolicyAttachmentDTO> expressAttachmentList = new ArrayList<>();
            List<PolicyAttachmentDTO> imgAttachmentList = new ArrayList<>();
            for (int i = 0; i < allAttachment.size(); i++) {
                PolicyAttachmentDTO policyAttachmentDTO = allAttachment.get(i);
                if (StrUtil.isNotEmpty(policyAttachmentDTO.getUrl()) && PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_CARGO_DESCRIPTION.equalsIgnoreCase(policyAttachmentDTO.getType())) {
                    cargoDescAttachmentList.add(policyAttachmentDTO);
                }
                if (StrUtil.isNotEmpty(policyAttachmentDTO.getUrl()) && PolicyAttachmentTypeConst.INSURANCE_ATTACHMENT_TRACKING_NUMBER.equalsIgnoreCase(policyAttachmentDTO.getType())) {
                    expressAttachmentList.add(policyAttachmentDTO);
                }
                if (StrUtil.isNotEmpty(policyAttachmentDTO.getUrl()) && PolicyAttachmentTypeConst.FRAGILE_GOODS_INTERNAL_PACKAGING_PHOTO.equalsIgnoreCase(policyAttachmentDTO.getType())) {
                    imgAttachmentList.add(policyAttachmentDTO);
                }
            }

            if (cargoDescAttachmentList != null && cargoDescAttachmentList.size() > 0) {
                String value = this.getJsonStr(cargoDescAttachmentList);
                if (StrUtil.isNotEmpty(value)) {
                    entity.setGoodsNoteFile(value);
                    entity.setGoodsNote("见附件");
                }
            }
            if (expressAttachmentList != null && expressAttachmentList.size() > 0) {
                String value = this.getJsonStr(expressAttachmentList);
                if (StrUtil.isNotEmpty(value)) {
                    entity.setExpressTrackingNumberFile(value);
                    entity.setExpressTrackingNumber("见附件");
                }
            }
            if (imgAttachmentList != null && imgAttachmentList.size() > 0) {
                entity.setGoodsPackageImages(this.getArray(imgAttachmentList));
            }
        }
    }

    private String getArray(List<PolicyAttachmentDTO> list) {
        if (list != null && list.size() > 0) {
            List<String> urlList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                PolicyAttachmentDTO policyAttachmentDTO = list.get(i);
                urlList.add(policyAttachmentDTO.getUrl());
            }
            return Arrays.toString(urlList.toArray());
        }
        return null;
    }

    private String getJsonStr(List<PolicyAttachmentDTO> list) {
        if (list != null && list.size() > 0) {
            JSONArray jsonArray = new JSONArray();
            for (int i = 0; i < list.size(); i++) {
                PolicyAttachmentDTO policyAttachmentDTO = list.get(i);
                String filePath = policyAttachmentDTO.getUrl();
                String originName = FileNameUtil.getName(filePath);
                JSONObject object = new JSONObject();
                object.put("originName", originName);
                object.put("name", filePath);
                object.put("url", filePath);
                object.put("uid", DateUtil.date().getTime());
                object.put("status", "success");
                jsonArray.add(object);
            }
            return jsonArray.toString();
        }
        return null;
    }


//    /**
//     * 附件处理
//     *
//     * @param entity
//     */
//    protected void getAttachment(JcPolicyDTO entity) {
//
//        if (StrUtil.isNotEmpty(entity.getGoodsPackageImages())) {
//            entity.setGoodsPackageImages(this.getArray(entity.getGoodsPackageImages()));
//        }
//        if (StrUtil.isNotEmpty(entity.getExpressTrackingNumberFile())) {
//            String value = this.getJson( entity.getExpressTrackingNumberFile());
//            if (StrUtil.isNotEmpty(value)) {
//                entity.setExpressTrackingNumberFile(value);
//                entity.setExpressTrackingNumber("见附件");
//            }
//        }
//        if (StrUtil.isNotEmpty(entity.getGoodsNoteFile())) {
//            String value = this.getJson( entity.getGoodsNoteFile());
//            if (StrUtil.isNotEmpty(value)) {
//                entity.setGoodsNoteFile(value);
//                entity.setGoodsNote("见附件");
//            }
//        }
//    }

//    private String getArray(String fileUrl) {
//        if (StrUtil.isNotEmpty(fileUrl)) {
//            String[] array = fileUrl.split(";");
//            return  Arrays.toString(array);
//        }
//        return null;
//    }
//
//    private String getJson(String fileUrl) {
//        if (StrUtil.isNotEmpty(fileUrl)) {
//            JSONArray jsonArray = new JSONArray();
//            String[] array = fileUrl.split(";");
//            for (int i = 0; i < array.length; i++) {
//                String filePath = array[i];
//                String originName = FileNameUtil.getName(filePath);
//                JSONObject object = new JSONObject();
//                object.put("originName", originName);
//                object.put("name", fileUrl);
//                object.put("url", fileUrl);
//                object.put("uid", DateUtil.date().getTime());
//                object.put("status", "success");
//                jsonArray.add(object);
//                return jsonArray.toString();
//            }
//        }
//        return null;
//    }

    public static void main(String[] args) {
        String s = "https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg;https://crossure.oss-cn-hangzhou.aliyuncs.com/policy/2025/08/21/b1105d9a742c4eeb912bd7d2d7573fa1/test.jpg";
        String[] array = s.split(";");
        System.out.println(Arrays.toString(array));
    }

}